package com.gege.service.impl;

import com.gege.mapper.OrderMapper;
import com.gege.model.Order;
import com.gege.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gechenpeng
 * @date 2021-05-22
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Order saveOrder(Order order) {
        orderMapper.insert(order);
        return order;
    }

    @Override
    public Order findOrderById(Long id) {
        return orderMapper.selectById(id);
    }

    @Override
    public List<Order> findByUserId(Long userId) {
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("user_id", userId);
        return orderMapper.selectByMap(paramMap);
    }

    @Override
    public void updateOrderStatusByUserId(Long userId) {
        int rows = orderMapper.updateOrderStatusByUserId(userId, (byte) 0);
        log.info("update {} rows", rows);
    }

    @Override
    public void removeByUserId(Long userId) {
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("user_id", userId);
        int rows = orderMapper.deleteByMap(paramMap);
        log.info("delete {} rows", rows);
    }
}
